package com.ecoolex.chinadutyfree.retailer.core.manager;

import com.ecoolex.chinadutyfree.retailer.core.dao.DirectMailGoodsOrderIdentityDAO;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrder;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderIdentity;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderIdentity.Column;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderIdentityExample;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderReceiver;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.Instant;

@Service
public class DirectMailGoodsOrderIdentityManager {
    @Autowired
    private DirectMailGoodsOrderIdentityDAO dao;

    @Autowired
    private DirectMailGoodsOrderReceiverManager directMailGoodsOrderReceiverManager;

    public DirectMailGoodsOrderIdentity getByOrderId(Integer orderId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(orderId, DirectMailGoodsOrderIdentity.NOT_DELETED);
    }
    
    public DirectMailGoodsOrderIdentity getPossibleBeDeletedByOrderId(Integer orderId) {
        return dao.selectByPrimaryKey(orderId);
    }

    public void deleteByOrderId(DirectMailGoodsOrder order, String updateUser) {
        DirectMailGoodsOrderIdentity record = new DirectMailGoodsOrderIdentity();
        record.setDeleted(DirectMailGoodsOrderIdentity.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DirectMailGoodsOrderIdentityExample example = new DirectMailGoodsOrderIdentityExample();
        example.createCriteria().andOrderIdEqualTo(order.getOrderId()).andDeletedEqualTo(
                DirectMailGoodsOrderIdentity.NOT_DELETED);

        dao.updateByExampleSelective(record, example, DirectMailGoodsOrderIdentity.Column.deleted, DirectMailGoodsOrderIdentity.Column.updateUser, DirectMailGoodsOrderIdentity.Column.updateTime);
    }

    public DirectMailGoodsOrderIdentity add(DirectMailGoodsOrder order, Integer needIdentityInfoType, DirectMailGoodsOrderIdentity record, String createUser) {
        if (needIdentityInfoType == 1 || needIdentityInfoType == 2) {
            // 姓名从收件人里面拿
            DirectMailGoodsOrderReceiver receiver
                    = directMailGoodsOrderReceiverManager.getByOrderId(order.getOrderId());
            record.setName(receiver.getReceiver());
            record.setOrderId(order.getOrderId());

            // 类型为1的不需要图片
            if (needIdentityInfoType == 1) {
                record.setFrontImage(null);
                record.setBackImage(null);
            }

            record.setDeleted(DirectMailGoodsOrderIdentity.NOT_DELETED);
            record.setCreateUser(createUser);
            record.setCreateTime(Date.from(Instant.now()));
            record.setUpdateUser(createUser);
            record.setUpdateTime(Date.from(Instant.now()));
            dao.insert(record);
        }

        return record;
    }

    public void verify(Integer needIdentityInfoType, DirectMailGoodsOrderIdentity record) {
        if (needIdentityInfoType == 1) {
            BizAssert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(20), "身份信息为空");
            BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getIdentityNo()), ResultCode.VERIFY.build(21), "身份证号码为空");
        } else if (needIdentityInfoType == 2) {
            BizAssert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(20), "身份信息为空");
            BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getIdentityNo()), ResultCode.VERIFY.build(21), "身份证号码为空");
            BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getFrontImage()),
                    ResultCode.VERIFY.build(22),
                    "身份证正面照片为空");
            BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getBackImage()), ResultCode.VERIFY.build(23), "身份证背面照片为空");
        }
    }
    
    public DirectMailGoodsOrderIdentity edit(DirectMailGoodsOrderIdentity record, String updateUser) {
    	// 姓名从收件人里面拿
        DirectMailGoodsOrderReceiver receiver
                = directMailGoodsOrderReceiverManager.getByOrderId(record.getOrderId());
        record.setName(receiver.getReceiver());
    	record.setUpdateUser(updateUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(record, Column.excludes(Column.deleted,Column.createUser,Column.createTime));
    	return record;
    }
}